#include <pthread.h>
#include <string>
#include <iostream>
#include <server/gfs.h>

using namespace std;

void * thread(void * arg)
{
    char * fname = static_cast<char *>(arg);
    for(int i = 0;i < 10;++i){
        CFile file(fname,O_WRONLY);
        if(!file.IsOpened()){
            cout<<"open write file error,create it\n";
            file.Create(fname,O_WRONLY);
            if(!file.IsOpened()){
                cout<<"create file error\n";
                return 0;
            }
        }
        char buf[10] = "12345678";
        if(file.Write(buf,sizeof buf) < 0){
            cout<<"write file error\n";
        }
        file.Close();
        cout<<"write file finished\n";
        file.Open(fname,O_RDONLY);
        if(!file.IsOpened()){
            cout<<"open read file error\n";
            return 0;
        }
        if(file.Read(buf,sizeof buf) < 0){
            cout<<"read file error\n";
        }else
            cout<<"read buf = "<<buf<<endl;
        sleep(3);
    }
    return 0;
}

int main(int argc,const char ** argv)
{
    pthread_t tid;
    pthread_create(&tid,0,thread,(void *)"/test.txt");
    pthread_join(tid,0);
}
